<?php
/**
* 2016年3月20日 16:13:19
* Message模型
*/
class Message_model extends MY_Model
{
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 发送消息
	 * @param  [type] $post [消息详情]
	 * @return [type] [description]
	 */
	function doSender($post)
	{
		$where = array(
				'del_status' => '0',
				'id'         => '1',
				);
		$from_phone = $this->hw_getRecord('member', $where, 'phone');
		$where = array(
				'phone'      => $post['to_phone'],
				'del_status' => '0',
				);
		$to_id = $this->hw_getRecord('member', $where, 'id');
		if (!$to_id) {
			return 1;
		}
		$data = array(
				'from_id'   => '1',
				'to_id'     => $to_id['id'],
				'from_phone' => $from_phone['phone'],
				'to_phone'   => $post['to_phone'],
				'title'     => $post['title'],
				'time'      => date('Y-m-d h:i:s', time()),
				'detail'    => $post['detail'],
				);
		$res = $this->hw_Insert('message', $data);
		if (!$res) {
			return 2;
		} else {
			return 3;
		}
	}

	/**
	 * 所有收到的消息
	 * @return [type] [description]
	 */
	function receiving()
	{
		$where = array(
				'to_id'      => '2',
				'del_status' => '0',
				);
		$message = $this->hw_getList('message', $where, 'id, title, time, status');
		return $message;
	}

	/**
	 * 消息详情
	 * @param  [type] $post [消息id]
	 * @return [type] [description]
	 */
	function messageDetail($post)
	{
		$where = array('id' => $post);
		$data = array('status' => '1');
		$this->hw_Update('message', $data, $where);
		$detail = $this->hw_getRecord('message', $where, 'from_phone, to_phone, title, time, detail');
		return $detail;
	}

	/**
	 * 消息删除
	 * @param  [type] $post [消息id]
	 * @return [type] [description]
	 */
	function messageDel($post)
	{
		$where = array('id' => $post);
		$data = array('del_status' => '1');
		$res = $this->hw_Update('message', $data, $where);
		return $res;
	}
}